<?php
class newsControl extends Control{
	 function index() {
        $db = K('newsview');
        $catid = isset($_GET['catid']) ? $_GET['catid'] : false;
        $where_catid = $catid ? "catid = '$catid'" : '';
        $where_status = isset($_GET['status']) ? "status='{$_GET['status']}'" : '';
        $count = $db->where($where_catid)->where($where_status)->where("recycled=0")->count();
        $page = new page($count, 8);
        $news = $db->join("category")->field("id,title,click,status,news.catid,cname,addtime")->
                        where($where_catid)->where($where_status)->where("recycled=0")->limit($page->limit())->all();
        $this->assign("news", $news);
        $this->assign("page", $page->show(2));
        $this->display();
    }
	//添加文章视图
    function add_show() {
        if (!isset($_GET['catid'])) {
            $catdb = M("category");
            $category = $catdb->field("catid,cname,pid")->all();
            $category = arr::channel($category, 'catid', 'pid', 0, 2);
            $this->assign("category", $category);
        }

        $this->display();
    }

    function add() {
        $db = K("newsrelation");
		$_POST['news_data']['userip'] = ip_get_client();
		$_POST['addtime'] = time();
		$_POST['updatetime'] = time();
		$_POST['thumb']=isset($_POST['thumb'][1][0])?$_POST['thumb'][1][0]:'';
        if ($db->add()) {
            $this->success("文章添加成功!  <a href='#'>返回栏目页</a>|<a href='#'>查看文章</a>|<a href='#'>继续添加文章</a>", '', 3);
        } else {
            $this->error("文章添加失败");
        }
    }

    //删除文章
    function del() {
        $id = $_GET['id'];
        $db = M("news");
        if ($db->save(array("id" => $id, "recycled" => 1))) {
            $this->success("文件已放入回收站");
        } else {
            $this->error("删除文章失败");
        }
    }

    //彻底删除文章
    function del_news() {
        $id = $_GET['id'];
        $news_db = K("newsrelation");
        if ($news_db->del($id)) {
            $this->success("删除成功");
        } else {
            $this->del("删除文章失败"
            );
        }
    }

    //回收站
    function recycled() {
        $db = K('newsview');
        $count = $db->where("recycled=1")->count();
        $page = new page($count, 2);
        $news = $db->join("category")->field("id,title,click,status,news.catid,cname,addtime")->
                        where("recycled=1")->limit($page->limit())->all();
        $this->assign("news", $news);
        $this->assign("page", $page->show(2));
        $this->display();
    }

    //还原
    function restore() {
        $id = $_GET['id'];
        $db = M("news");
        if ($db->save(array("id" => $id, "recycled" => 0))) {
            $this->success("文章已还原");
        } else {
             $this->error("还原文章失败，请重试");
        }
    }

  //编辑文章
    public function edit_show(){
          $db =M('news');
          $cid=isset($_GET['id'])?$_GET['id']:false;
         $news=$db->where('id='.$cid)->all();
         $da=M('category');
         $category=$da->all();
         $this->assign('category',$category);
         $this->assign("news", $news);
         $this->display();
  	
  }
 // 编辑文章接收的更新
  public function update(){
  	$db=k('newsview');
  	$cid=$_POST['id'];
     if($db->where('id='.$cid)->update()){
  		$this->success('更新成功');
  	}else{
  	 $this->error('更新失败');
     }
  }
//后台显示未审核的
public  function reply(){
	$db=K('commentview');
	$cun=$db->count('stat=0');
    $page=new page($cun,5);
    $reply=$db->where("stat=0")->findall($page->limit());
	$this->assign('reply',$reply);
	$this->assign('page',$page->show());
	$this->display();
}

public function abc(){
	$db=M("comment");
	$cid=$_GET["ids"];
    $sql = "update $db->tableName set stat=1 where cid in ($cid)";
    $affect_row = $db->exe($sql);
    if($affect_row>0){
    	$this->success('审核成功');
    }else{
    	$this->success('审核失败');
    }

	
	   

	
    	
}



}

?>
	